'use strict';

const Controller = require('egg').Controller;

const moment = require('moment');

class ScoreController extends Controller {
    //创建
    async create() {
        let {ctx,service} = this;
        let {username,theory,skill} = ctx.request.body;
        if(username && theory && skill){
            let timer = moment(new Date()).format('YYYY-MM-DD'); //时间 2021-04-29 moment
            try{
                await service.score.add(username,theory,skill,timer);
                ctx.body = {
                    code:1,
                    msg:'添加成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:'添加失败'
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }

    //删除
    async destroy(){
        let {ctx,service} = this;
        let {id} = ctx.params;
        if(id){
            try{
                await service.score.destroy(id);
                ctx.body = {
                    code:1,
                    msg:'删除成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:'删除失败'
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }

    //修改
    async update(){
        let {ctx,service} = this;
        let {theory,skill} = ctx.request.body;
        let {id} = ctx.params;
        if(id && theory && skill){
            try{
                await service.score.update(theory,skill,id);
                ctx.body = {
                    code:1,
                    msg:'修改成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:'修改失败'
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }

    //查询
    async index(){
        //查询当天成绩列表
        let curTime = moment(new Date()).format('YYYY-MM-DD');
        let list = await this.service.score.index(curTime);
        this.ctx.body = {
            code:1,
            data:list
        }
    }

    async notScore(){
        let {ctx,service} = this;
        let curTime = moment(new Date()).format('YYYY-MM-DD');

        //当天没有录入成绩的人员名单
        let result = await service.score.notScore(curTime);
        ctx.body = {
            code:1,
            data:result
        }
    }

    //搜索
    async search(){
        let {ctx,service} = this;
        let key = ctx.query.key;
        let curTime = moment(new Date()).format('YYYY-MM-DD');

        let target = await service.score.search(key,curTime);
        ctx.body = {
            code:1,
            data:target
        }
    }
}

module.exports = ScoreController;
